package com.zhongxi.guoyuan.adapter.rewardCoin;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.OffLineShop.Adapter.ShopListAdapter;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinStore.HotGoodsData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;
import com.zhongxi.guoyuan.utils.Constants;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.widget.screenadaptaion.CustomLinearlayout;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/26.
 */

public class RewardItemCateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_FOOT = 1;
    private static final int TYPE_ITEM = 2;
    /**
     * 加载中
     */
    public static final int LOADING = 3;
    /**
     * 加载完成
     */
    public static final int LOADCOMPLENT = 4;
    /**
     * 加载到底
     */
    public static final int LOADEND = 5;
    private List<HotGoodsData> dataList;
    private int loadingState;

    public RewardItemCateAdapter(List<HotGoodsData> dataList) {
        this.dataList = dataList;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == dataList.size()) {
            return TYPE_FOOT;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOT) {
            return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.loadmore_footer, parent, false));
        } else {
            return new CateViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_reward_item_cate, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof CateViewHolder) {
            final CateViewHolder mCateViewHolder = (CateViewHolder) holder;
            HotGoodsData mHotGoodsData = dataList.get(position);
            mCateViewHolder.mTvName.setText(mHotGoodsData.getName());
            Glide.with(UiUtils.getContext())
                    .load(Url.IMG_URL + mHotGoodsData.getThumbnail())
                    .placeholder(R.mipmap.iv_loading)
                    .into(mCateViewHolder.mImageView);

            mCateViewHolder.mTvSalesVolume.setText("销量:" + mHotGoodsData.getOrdernum());
            mCateViewHolder.mTvPrice.setText(mHotGoodsData.getFloorprice() + " 元");
            switch (mHotGoodsData.getGoodstype()) {
                case "1":
                    break;
                case "2":
                    break;
                case "3":
                    if (DataUtil.moreThanZero(CommonTool.getWhole(DataUtil.multiplys(mHotGoodsData.getFloorprice(), Constants.REDBEAN_RATE, 4)))) {
                        mCateViewHolder.mTvHd.setText(UiUtils.getString(R.string.award_hongdou) + CommonTool.getWhole(DataUtil.multiplys(mHotGoodsData.getFloorprice(), Constants.REDBEAN_RATE, 4)) + " 红豆");

                    } else {
                        mCateViewHolder.mTvHd.setText("");

                    }
                    mCateViewHolder.mTvCase.setText(UiUtils.getString(R.string.goldenDesc) + mHotGoodsData.getFloorprice() + "元消费红包");
                    break;
            }
            mCateViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(mCateViewHolder.itemView, position);
                    }
                }
            });

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder mHolder = (FooterViewHolder) holder;
            switch (loadingState) {
                case LOADING:  // 正在加载
                    mHolder.mLoading.setVisibility(View.VISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADCOMPLENT:  // 加载完成
                    mHolder.mLoading.setVisibility(View.INVISIBLE);
                    mHolder.mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADEND:  //  记载到底
                    mHolder.mLoading.setVisibility(View.GONE);
                    mHolder.mLoadComplent.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (dataList != null && dataList.size() > 0) {
            return dataList.size() + 1;
        }
        return 0;
    }

    public void setLoadingState(int loadingState) {
        this.loadingState = loadingState;
        notifyDataSetChanged();
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(View view, int position);
    }

    class CateViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_name)
        TextView mTvName;
        @Bind(R.id.imageView)
        ImageView mImageView;
        @Bind(R.id.tv_salesVolume)
        TextView mTvSalesVolume;
        @Bind(R.id.tv_case)
        TextView mTvCase;
        @Bind(R.id.tv_hd)
        TextView mTvHd;
        @Bind(R.id.tv_price)
        TextView mTvPrice;

        public CateViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ll_loading)
        CustomLinearlayout mLoading;
        @Bind(R.id.tv_loadcomplent)
        TextView mLoadComplent;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
